package com.hsmpay.mobile.action.added;

import com.hsmpay.common.action.BaseAction;
import com.hsmpay.common.pojo.user.User;
import com.hsmpay.common.util.DateConvertUtil;
import com.hsmpay.common.util.NumberUtil;
import com.hsmpay.common.util.URLDecoderTools;
import com.hsmpay.mobile.pojo.added.AddedOrder;
import com.hsmpay.mobile.service.added.AddedOrderService;
import com.hsmpay.mobile.service.user.UserService;
import com.hsmpay.utils.Des3;
import it.sauronsoftware.base64.Base64;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 千48交易凭条 action
 *
 * @author 颜铃璋
 * @date 2014-5-6
 */
@Controller("addedCertificateAction")
@Scope("prototype")
public class AddedCertificateAction extends BaseAction {
    private static final long serialVersionUID = -4742882068458752958L;
    private static Logger log = Logger.getLogger(AddedCertificateAction.class);
    private Long psId;//收款Id
    private Long terminalTypeId;//终端类型
    private String d;//传输数据

    @Resource(name = "addedOrderService")
    protected AddedOrderService<AddedOrder, Long> addedOrderService;
    @Resource(name = "userService")
    protected UserService<User, Long> userService;

    public Map<String, Object> certificateMap;

    /**
     * 凭证 http://localhost:8080/poc/c.action?d=NF80OTQxNg%3D%3D
     *
     * @return
     * @throws Exception
     */
    public String searchCertificate() throws Exception {
        if (StringUtils.isNotBlank(d)) {//使用加密
            d = URLDecoderTools.decodeUTF8(d);
            String dStr = new String(Des3.decodeXP(d));
            String[] dStrArray = dStr.split("_");
            terminalTypeId = Long.parseLong(dStrArray[0]);
            psId = Long.parseLong(dStrArray[1]);
        }

        if (null == psId || null == terminalTypeId) {
            log.warn("凭证, 传入参数password为空");
            return null;
        }

        try {
            AddedOrder addedOrder = new AddedOrder();
            addedOrder.setId(psId);
            addedOrder.setStatus(1);
            addedOrder = addedOrderService.searchEntity(addedOrder);
            if (null != addedOrder) {
                certificateMap = new HashMap<String, Object>();

                User user = userService.getUserById(addedOrder.getUserId());
//				addedOrder.setUserName(user.getUserName());

                String acquirersName = user.getOpeningBank();//修改收单行
                if (null != acquirersName) {
                    if (!acquirersName.equals("中国银行"))
                        acquirersName = acquirersName.replace("中国", "");
//						addedOrder.setAcquirersName(acquirersName);
                }
//					transPriorSettle.setAcquirersName("交通银行");

//					String returnResult = "certificate";
//					switch(terminalTypeId.intValue()){
//						case 1:break;//花生米
//						case 2:break;//益付
//						case 6:break;//移动支付
//						//case 4:returnResult += "_" + tType;;//智钱
//						//case 7:returnResult += "_" + tType;;//移动支付
//						default : returnResult += "_ttype";
//					}
                    /*if(1l != tType.longValue() && 2l != tType.longValue() && 6l != tType.longValue() && 6l != tType.longValue()){
						returnResult += "_" + tType;
					}*/

//					getRequest().setAttribute("terminalTypeId", terminalTypeId);
//					getRequest().setAttribute("addedOrder", addedOrder);
//					getRequest().setAttribute("orderDate", DateConvertUtil.dateString(addedOrder.getCreateDate(), "yyyy/MM/dd"));
//					getRequest().setAttribute("orderTime", DateConvertUtil.dateString(addedOrder.getCreateDate(), "HH:mm:ss"));

                String authCode = addedOrder.getAuthorizationCode();
                String searchId = addedOrder.getSearchId();
                if (15 == addedOrder.getChannelId()) {
                    //authCode = "000"+authCode.substring(authCode.length()-3, authCode.length());
                    searchId = authCode;
                    authCode = "000000";
                }

                certificateMap.put("userName", user.getUserName());//商户名称(MERCHANT NAME)
                certificateMap.put("merchantId", addedOrder.getMerchantId());//商户号(MERCHANT NO)
                certificateMap.put("terminalId", addedOrder.getTerminalId());//终端号(TERMINAL NO)  user.getId()
                certificateMap.put("bankCard", NumberUtil.cardXing(addedOrder.getBankCard()));//卡号(CARD NO)
                certificateMap.put("acquirersName", acquirersName);//收单行名(ACQUIRER)
                certificateMap.put("issueBank", addedOrder.getIssueBank());//发卡行名(ISSUER)
                certificateMap.put("batchId", addedOrder.getBatchId());//批次号(BATCH NO)
                certificateMap.put("searchId", "".equals(searchId) ? "000000" : searchId);//凭证号(VOUCHER NO)
                certificateMap.put("authorizationCode", "".equals(authCode) ? "000000" : authCode);//授权码(AUTH NO)
                certificateMap.put("bankRef", addedOrder.getBankRef());//参考号(REFER NO)

                certificateMap.put("transDate", DateConvertUtil.dateString(addedOrder.getCreateDate(), "yyyy/MM/dd"));//交易日期(DATE)
                certificateMap.put("transTime", DateConvertUtil.dateString(addedOrder.getCreateDate(), "HH:mm:ss"));//交易日期(DATE)
                certificateMap.put("transMoney", addedOrder.getTransMoney());//金额(AMOUNT)
                certificateMap.put("signaturePath", addedOrder.getSignaturePath());//持卡人签名(CARDHOLDER SINGATURE)

                return "certResult";
            }

        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
        return null;
    }


    public static void main(String args[]) {
        String str = 6 + "_" + 23;
        System.out.println(URLDecoderTools.encodeUTF8(Base64.encode(str)));
    }

    public void setD(String d) {
        this.d = d;
    }


    public Map<String, Object> getCertificateMap() {
        return certificateMap;
    }
}
